﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.IO;

namespace Pavilion.DesignerTool
{
    public class TextureData
    {
        public CTexture2D Texture { get; set; }
        public bool Visible { get { return Texture.Visible; } set { Texture.Visible = value; } }
        public UndoRedoController UndoRedoController { get; private set; }

        private Texture2D OriginalTexture { get; set; }
        private string OriginalFileName { get; set; }
        public string DisplayName { get; set; }

        private bool IsTextureChanged { get { return (Texture.TextureItem != OriginalTexture); } }
        private bool IsFileNameChanged(string newFileName) { return (newFileName != OriginalFileName); }
        
        public TextureData(CTexture2D texture, string originalFileName)
        {
            this.Texture = texture;
            OriginalTexture = Texture.TextureItem;
            DisplayName = OriginalFileName = originalFileName;

            UndoRedoController = new UndoRedoController();
        }

        public void Save(string fullFileName, string fileName)
        {
            fullFileName += fileName + ".png";

            if (File.Exists(fullFileName) && !IsTextureChanged && !IsFileNameChanged(fileName) )
                return;

            Stream stream = new FileStream(fullFileName, FileMode.Create);
            Texture.TextureItem.SaveAsPng(stream, Texture.TextureItem.Width, Texture.TextureItem.Height);
            OriginalTexture = Texture.TextureItem;
            DisplayName = OriginalFileName = fileName;
        }
    }
}
